package com.cedric.yixianpai.game.Status;

import com.cedric.yixianpai.game.Constants.CardType;
import lombok.extern.slf4j.Slf4j;

/**
 * 对局数据
 * 包括 玩家名称、玩家状态buff,玩家实时对局血量，回合停留
 * note:一些特殊构造，不要用注解代替
 */
@Slf4j
public class DuelStatus {
    private String playerName;
    private DuelBuff playerBuff;
    private DuelHp playerHp;
    private boolean turnStop;//回合停留
    private boolean again;//再动


    public DuelStatus(String playerName,int playerHp) {
        this.playerName = playerName;
        this.playerBuff = new DuelBuff();
        this.playerHp = new DuelHp(playerHp);
        this.turnStop = false;
        this.again=false;
    }

    public String getPlayerName() {
        return playerName;
    }

    public void setPlayerName(String playerName) {
        this.playerName = playerName;
    }

    public DuelBuff getPlayerBuff() {
        return playerBuff;
    }

    public void setPlayerBuff(DuelBuff playerBuff) {
        this.playerBuff = playerBuff;
    }

    public DuelHp getPlayerHp() {
        return playerHp;
    }

    public void setPlayerHp(DuelHp playerHp) {
        this.playerHp = playerHp;
    }

    public boolean survive() {
        return this.getPlayerHp().survive();
    }

    public boolean isTurnStop() {
        return turnStop;
    }

    public void setTurnStop() {
        this.turnStop = true;
    }

    public void cancelTurnStop() {
        this.turnStop = false;
    }

    public void setAgain() {
        this.again = true;
    }

    public void resetAgain() {
        this.again = false;
    }

    public boolean isAgain(){
        return this.again;
    }


    /**
     * 判断灵气是否足够
     * 若足够会先消耗
     *
     * @param need 需要的灵气
     * @return 是否充足
     */
    public boolean spiritEnough(int need) {
        if (need <= this.playerBuff.getSpiritualEnergy()) {
            this.getPlayerBuff().decSpiritualEnergy(need);
            return true;
        } else {
//            System.out.println("灵气不足！");//todo:暂停回合
            this.setTurnStop();
            this.playerBuff.addSpiritualEnergy(1);
            return false;
        }
    }

    /**
     * 单次攻击 计算了剑意但没清空剑意 处理了盾
     *
     * @param enemyS 敌方状态
     * @param damage 伤害量
     * @return 击伤数值
     */
    public int attack(DuelStatus enemyS, int damage) {
        int sumDamage = damage + this.playerBuff.getSwordIntent()+this.playerBuff.getAttackAdd();//剑意，加攻
        int sheild = enemyS.getPlayerBuff().getShield();//简称
        if (this.getPlayerBuff().getIgnoreDefense() > 0) {//是否无视防御
            enemyS.playerHp.decHp(sumDamage);
            this.getPlayerBuff().decIgnoreDefense();
//            log.info("        " + enemyS.getPlayerName() + "受到" + sumDamage + "伤害");
            return sumDamage;
        } else {
            if (sheild >= sumDamage) {
                enemyS.playerBuff.decShield(sumDamage);
//               log.info("        " + enemyS.getPlayerName() + "护盾抵挡" + sumDamage + "点伤害");
                return 0;
            } else {
                enemyS.playerHp.decHp(sumDamage - sheild);
//                if (sheild != 0) {
//                    log.info("        " + enemyS.getPlayerName() + "护盾抵挡" + sheild + "点伤害");
//                }
//               log.info("        " + enemyS.getPlayerName() + "受到" + (sumDamage - sheild) + "伤害");
                enemyS.getPlayerBuff().setShield(0);
                return sumDamage-sheild;
            }
        }
    }

    public boolean lastIs(CardType type) {
        if (this.getPlayerBuff().getLastUseType() == type) {
            return true;
        }
        return false;
    }

    public void heal(int heal) {
        if (heal >= 0) {
            this.playerHp.addHp(heal);
            return;
        }
        throw new RuntimeException("Heal value can't less than 0 Error!");
    }
}
